/*
 *         Copyright (c) KUBOTA Corporation and HUDSON SOFT,
 *                  1994, 1995 All Rights Reserved
 *
 *  This software comprises unpublished confidential information of
 *  KUBOTA Corp. and HUDSON SOFT. It may not be used, copied or made 
 *  available to anyone, except in accordance with the license under
 *  which it is furnished.
 */
static char SccsId[] = "@(#)move.c	1.6	9/19/95";
#include <farl.h>

extern unsigned int butaaic[];
extern unsigned int butaagc[];
#define BUTA_AIC_SIZE 33426
#define BUTA_AGC_SIZE 3084

short	buta_zoom_x, buta_zoom_y, buta_zoom_z;
short	buta_rot_x, buta_rot_y, buta_rot_z;
short	buta_off_x, buta_off_y, buta_off_z;
short	buta_delta_x, buta_delta_y, buta_delta_z;
short	buta_direct[2][2] = {
     2250,			/* z = 0, x = 0 */
     1350,			/* z = 0, x = 1 */
     3150,			/* z = 1, x = 0 */
     450,			/* z = 1, x = 1 */
};

#define DEFAULT_ZOOM 0x80
#define DEFAULT_BOTTOM 0xffa6	/* -0.6 */
#define SWAP_WINDOW() {FarlSwapWindow();}
#define dispbuta() { \
			  mxsetbuta(); \
			  FarlWriteFifoWA(butaagc, BUTA_AGC_SIZE);\
		}

void
instbuta()
{
     FarlWriteFifoWA(butaaic, BUTA_AIC_SIZE);

     buta_zoom_x = DEFAULT_ZOOM;
     buta_zoom_y = DEFAULT_ZOOM;
     buta_zoom_z = DEFAULT_ZOOM;
     buta_off_x = 0x0;
     buta_off_y = DEFAULT_BOTTOM;
     buta_off_z = 0xff00;
     buta_delta_x = 1;
     buta_delta_y = 0;
     buta_delta_z = 1;

     buta_rot_x = 900;
     buta_rot_y = buta_direct[(buta_delta_z+1)/2][(buta_delta_x+1)/2];
     buta_rot_z = 0;
}

void
mxsetbuta()
{
     FarlSetUnitMatrix187();
     FarlRotateX4x4M187(buta_rot_x);
     FarlCopyDstToSrc();
     FarlRotateY4x4M187(buta_rot_y);
     FarlCopyDstToSrc();
     FarlRotateZ4x4M187(buta_rot_z);
     FarlCopyDstToSrc();
     FarlScale4x4M187(buta_zoom_x, buta_zoom_y, buta_zoom_z);
     FarlCopyDstToSrc();
     FarlTranslate4x4M187(buta_off_x, buta_off_y, buta_off_z);
     FarlCopyDstToSrc();
     FarlPerseZ4x4M187(0xffa0);
     FarlCopyDstToObj();

     FarlSetUnitMatrix1015();
     FarlRotateX3x3M1015(buta_rot_x);
     FarlCopyDstToSrc();
     FarlRotateY3x3M1015(buta_rot_y);
     FarlCopyDstToSrc();
     FarlRotateZ3x3M1015(buta_rot_z);
     FarlCopyDstToNrm();
}

#define V0	300
#define G0	10
void jumpbuta()
{
     register int	h;
     register int	t = 0;
     static short zoom_y[4] = { 32, 48, 64, 80};

     for (t = 0; t < 4; t++) {
	  buta_zoom_y = DEFAULT_ZOOM - zoom_y[t];
	  buta_off_y = FarlFix187(-0.5) - zoom_y[t]/4;
	  dispbuta();
	  SWAP_WINDOW();
     }
     buta_zoom_y = DEFAULT_ZOOM;
     for (t = 0; t <= 60; t++) {
	  buta_off_x += buta_delta_x;
	  if (0x68 < buta_off_x || buta_off_x < (0 - 0x68)) {
	       buta_delta_x = -buta_delta_x;
	       buta_rot_y = buta_direct[(buta_delta_z+1)/2][(buta_delta_x+1)/2];
	  }
	  buta_off_z += buta_delta_z;
	  if ((0 - 0x71) < buta_off_z || buta_off_z < (0 - 0x200) ) {
	       buta_delta_z = -buta_delta_z;
	       buta_rot_y = buta_direct[(buta_delta_z+1)/2][(buta_delta_x+1)/2];
	  }

	  h = V0 * t - G0 / 2 * t * t;
	  buta_off_y = FarlFix187(-0.5 + (float)h/4500);
	  dispbuta();
	  SWAP_WINDOW();
     }
}

void
crushbuta()
{
     register int	t;
     static short crush_y[4] = { 32, 64, 96, 128};

     for (t = 0; t < 4; t++) {
	  buta_zoom_y = DEFAULT_ZOOM - crush_y[t];
	  buta_off_y = FarlFix187(-0.5) - crush_y[t]/4;
	  dispbuta();
	  SWAP_WINDOW();
     }
     for (t = 0; t < 60; t++){
	  dispbuta();
	  SWAP_WINDOW();
     }

     for (t = crush_y[3] - 1; t >= 0; t -= (crush_y[3] / 32)) {
	  buta_zoom_y = DEFAULT_ZOOM - t;
	  buta_off_y = FarlFix187(-0.5) - t/4;
	  dispbuta();
	  SWAP_WINDOW();
     }
     for (t = 0; t < 60; t++){
	  dispbuta();
	  SWAP_WINDOW();
     }
}

void
turnbuta()
{
     register int	t;

     for (t = 0; t < 30; t++) {
	  buta_rot_y -= 20;
	  if (buta_rot_y < 0)	buta_rot_y +=3600;
	  dispbuta();
	  SWAP_WINDOW();
     }
     for (t = 0; t < 80; t++) {
	  buta_rot_y += 450;
	  if (buta_rot_y >= 3600)	buta_rot_y %= 3600;
	  dispbuta();
	  SWAP_WINDOW();
     }

     for (t = 0; t < 11; t++) {
	  buta_rot_y += 200;
	  if (buta_rot_y >= 3600)	buta_rot_y %= 3600;
	  dispbuta();
	  SWAP_WINDOW();
     }
     for (t = 0; t < 10; t++) {
	  buta_rot_y += 100;
	  if (buta_rot_y >= 3600)	buta_rot_y %= 3600;
	  dispbuta();
	  SWAP_WINDOW();
     }
     for (t = 0; t < 10; t++) {
	  buta_rot_y += 50;
	  if (buta_rot_y >= 3600)	buta_rot_y %= 3600;
	  dispbuta();
	  SWAP_WINDOW();
     }
     for (t = 0; t < 15; t++) {
	  buta_rot_y += 20;
	  if (buta_rot_y >= 3600)	buta_rot_y %= 3600;
	  dispbuta();
	  SWAP_WINDOW();
     }
     for (t = 0; t < 20; t++) {
	  buta_rot_y += 10;
	  if (buta_rot_y >= 3600)	buta_rot_y %= 3600;
	  dispbuta();
	  SWAP_WINDOW();
     }
}
